package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dbOperator.AdvancedOperation ;
/**
 * Servlet implementation class EditorFine
 */
@WebServlet("/EditorFine")
public class EditorFine extends HttpServlet {
	private static final long serialVersionUID = 1L;


	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置请求编码、响应方式和编码方式
		response.setContentType("text/html; charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		double Fine=Double.valueOf(request.getParameter("Fine"));
		double SecurityDeposit=Double.valueOf(request.getParameter("SecurityDeposit"));
		int ReturnPeriod=Integer.parseInt(request.getParameter("ReturnPeriod"));
		request.setAttribute("Fine", Fine);
		request.setAttribute("SecurityDeposit", SecurityDeposit);
		request.setAttribute("ReturnPeriod", ReturnPeriod);
		//int updaterUid =getAttribute(uid);//更新者uid
		int updaterUid=-1;
		/*try {
			updaterUid = dbOperator.AdvancedOperation.getUid("qwer@126.com");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}//屏蔽cookie*/
		
		
		
		Cookie cookies[] = request.getCookies();
		if(cookies == null) {response.sendRedirect("index.html");return;}
		Connection con = dbOperator.BasicOperation.getConnection();
		for(Cookie c : cookies) {
			if(c.getName().equals("LoginInfo")) {
				String currentCookie = c.getValue();
					try {
						updaterUid = AdvancedOperation.getUidbyCookie(con,currentCookie);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						try {con.close();} catch (SQLException e1) {e.printStackTrace();}
						response.sendRedirect("index.html");return;
					}
					if(updaterUid == -1) {response.sendRedirect("index.html");
											try {con.close();} catch (SQLException e1) {e1.printStackTrace();}
											return;}
						}
					}
		try {
			AdvancedOperation.updateFineValue(con,updaterUid, Fine);
			AdvancedOperation.updateSecurityDeposit(con,updaterUid, SecurityDeposit);
			AdvancedOperation.updateReturnPeriod(con,updaterUid, ReturnPeriod);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {con.close();} catch (SQLException e1) {e1.printStackTrace();}
			e.printStackTrace();
		}
		request.removeAttribute("Fine");
		request.removeAttribute("SecurityDeposit");
		request.removeAttribute("ReturnPeriod");
		response.sendRedirect(request.getHeader("Referer"));
		try {con.close();} catch (SQLException e1) {e1.printStackTrace();}
		return;
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
